﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdvancedCSharpFeatures
{
    public static class IEnumerableExtensions
    {
        public static T Min<T>(this IEnumerable<T> collection) where T : IComparable<T>
        {
            collection.ToList();

            if (collection == null || collection.Count() == 0)
            {
                throw new ArgumentException("The collection cannot be null");
            }
            T min = default(T);
            foreach (var item in collection)
            {
                min = item;
                break;
            }
            foreach (var item in collection)
            {
                if (item.CompareTo(min) < 0) min = item;
            }
            return min;
        }

        public static T Max<T>(this IEnumerable<T> collection) where T : IComparable<T>
        {
            collection.ToList();

            if (collection == null || collection.Count() == 0)
            {
                throw new ArgumentException("The collection cannot be null");
            }
            T max = default(T);
            foreach (var item in collection)
            {
                max = item;
                break;
            }
            foreach (var item in collection)
            {
                if (item.CompareTo(max) > 0) max = item;
            }
            return max;
        }
    }
}
